package com.zhao.controller;

import com.zhao.Others.FileOperationObject;
import com.zhao.pojo.EbickGoods;
import com.zhao.pojo.OrderForm;
import com.zhao.pojo.Student;
import com.zhao.service.EbickGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@Controller
public class EbickGoodsController {

    @Autowired
    private EbickGoodsService ebickGoodsService;

    private List<String> imagePathList = new ArrayList<>();
    private Lock l = new ReentrantLock();



    @RequestMapping("student-seeAllEbickGoods.do")
    public String studentSeeAllEbickGoods(HttpSession session){
        Student student = (Student)session.getAttribute("student");
        if(student == null){
            return "redirect:/";
        }

        String res = ebickGoodsService.studentSeeAllEbickGoods(student,session);
        System.out.println("res-studentSeeAllEbickGoods: "+res);
        if(!res.equals("success")){
            return "404";
        }

        ////////////////////////////////

        return "redirect:/newStudent-seeAllEbickGoods";
    }

    @RequestMapping("student-toUpdateEbickGoods.do")
    public String studentToUpdateEbickGoods(String ebickID ,HttpSession session){

        Student student = (Student)session.getAttribute("student");
        if(student == null){
            return "redirect:/";
        }
        String res = ebickGoodsService.studentToUpdateEbickGoods(student,ebickID, session);
        System.out.println(res);
        if(!res.equals("success")){
            return "404";
        }

        return "redirect:/newStudent-updateEbickGoods";
    }

    @ResponseBody
    @RequestMapping("student-updateEbickGoods.do")
    public String studentUpdateEbickGoods(String ebickID ,String price,String details,String labels,HttpSession session){

        Student student = (Student)session.getAttribute("student");
        if(student == null){
            return "redirect:/";
        }
        EbickGoods ebickGoods = (EbickGoods)session.getAttribute("needUpdateEbickGoods");
        ebickGoods.setPrice(price);
        ebickGoods.setDetails(details);
        ebickGoods.setLabels(labels);
        String res = ebickGoodsService.studentUpdateEbickGoods(student,ebickGoods,session);
        if(!res.equals("success")){
            return "404";
        }
        session.removeAttribute("needUpdateEbickGoods");
        return res;
    }

    @RequestMapping("student-deleteEbickGoods.do")
    public String studentDeleteEbickGoods(String ebickID ,HttpSession session){

        Student student = (Student)session.getAttribute("student");
        if(student == null){
            return "redirect:/";
        }
        String res = ebickGoodsService.studentDeleteEbickGoods(student,ebickID, session);
        System.out.println(res);
        if(!res.equals("success")){
            return "404";
        }

        return "redirect:/student-getAllMyEbickGoods.do";
    }


    @RequestMapping("student-getAllMyEbickGoods.do")
    public String getAllMyEbickGoods(HttpSession session){
        Student student = (Student)session.getAttribute("student");
        if(student == null){
            return "redirect:/";
        }
        String res = ebickGoodsService.studentGetAllMyEbickGoods(student,session);
        System.out.println("res-getAllMyEbickGoods: "+res);
        return "redirect:/newStudent-allMyEbickGoods";
    }

    @ResponseBody
    @RequestMapping("uploadEbickGoodsImage.do")
    public String uploadEbickGoodsImage(MultipartFile file, HttpSession session){
        Student student = (Student)session.getAttribute("student");

        l.lock();

        String visitPath = FileOperationObject.savePicture(file,"goodsPicture",student.getUniversityID(),student.getStudentID());
        l.unlock();
        imagePathList.add(visitPath);
        return "yes";
    }

    @ResponseBody
    @RequestMapping("student-uploadGoodsEbick.do")
    public String studentUploadGoodsEbick(String ebickID,String price,String details,String labels,HttpSession session ){

        Student student = (Student)session.getAttribute("student");
        if(student == null) {
            return "请先登录";
        }
        EbickGoods ebickGoods = new EbickGoods();
        ebickGoods.setEbickID(ebickID);
        ebickGoods.setPrice(price);
        ebickGoods.setDetails(details);
        ebickGoods.setLabels(labels);
        session.removeAttribute("goodsEbickID");
        String res = ebickGoodsService.studentUploadEbickGoods(student,ebickGoods,imagePathList,session);
        System.out.println("res-udentUploadEbickGoods: "+res);
        imagePathList.clear();
        
        return res;
    }

    @RequestMapping("student-to-uploadGoodsEbick.do")
    public String tostudentUploadGoodsEbick(String ebickID,HttpSession session ){

        Student student = (Student)session.getAttribute("student");
        if(student == null) {
            return "请先登录";
        }
        session.setAttribute("goodsEbickID",ebickID);
        imagePathList.clear();


        return "redirect:/newStudent-uploadEbickGoods";
    }

    @RequestMapping("tostudent-uploadGoodsEbick.do")
    public String toUploadGoodsEbick(HttpSession session ){

        Student student = (Student)session.getAttribute("student");
        if(student == null) {
            return "请先登录";
        }
        session.removeAttribute("goodsEbickID");
        imagePathList.clear();


        return "redirect:/newStudent-uploadEbickGoods";
    }


}
